package SEApp.boxes.albums
{
	import SEApp.components.se_source;
	
	import UIFree.controls.uif_hscrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;

	public class se_previewSource extends se_source {
		private var _mask: uif_ecomponent;
		private var _url: String;
		private var _index: int; 
		
		public function se_previewSource(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			visible = false;
			_index = scrollArea.scrollArea.numChildren - 1;
		}
		
		public function get scrollArea(): Object {
			return (parent.parent.parent)?(parent.parent.parent as uif_hscrollArea):{};
		}
		
		protected function doLoadFromScroll(e: Event): void {
			if (parent.parent) {
				if (scrollArea.checkVisible(_index)) {
					loadSource(_url);
					scrollArea.removeEventListener(Event.SCROLL, doLoadFromScroll);
				}
			} else {
				trace('no scrollArea');
			}
		}
		
		override public function loadSource(url: String): void {
			_url = url;
			var s: int = -scrollArea.startIndex * scrollArea.rows;
			if (scrollArea.checkVisible(_index))
				super.loadSource(_url);
			else addEvent(Event.SCROLL, doLoadFromScroll, scrollArea as EventDispatcher);
		}
		
		override protected function doLoad(e:Event):void {
			if (parent && parent.hasOwnProperty('fitToInner')) {
				if (!_mask) {
					var inner: Rectangle = getInner();
					_mask = new uif_ecomponent(this, inner.x, inner.y, inner.width, inner.height, 'white');
				}
				if ((_source.width < _mask.width) || 
					(_source.height < _mask.height)) {
					var kof: Number = _source.height / _source.width;
					if (kof > 1) {
						_source.width  = _mask.width;
						_source.height = _mask.width * kof;
					} else {
						_source.height = _mask.width;
						_source.width  = _mask.width / kof;
					}
				}
				_source.x = _mask.x + (_mask.width  - _source.width) / 2; 
				_source.y = _mask.y + (_mask.height - _source.height) / 2; 
				_source.mask = _mask;
			} else super.doLoad(e);
			slideShow();
//			fx('scaleCenter', 0.1, 1).start();
		}
	}
}